package com.bilibili.sys.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author alpha
 * @program: BookSystem
 * @description: 分页的工具类
 * @since 2024-03-27 16:31
 **/

public class PageUtils {
    private String key;//查询的关键字
    private int pageSize = 5;//每页显示的条数
    private int pageNum = 1;//第几页 默认查询第一页
    private int totalCount;//总的记录数
    private int totalPage;//总的页数
    private List<?> list;//分页显式的数据
    private List<String> pageList;//前端分页的页码列表

/**
     * 分页显示 1 2 3 ... 7 8 9
     * 1
     * 1 2
     * 1 2 3
     * 1 2 3 4
     * 1 2 3 4 5
     * 1 2 3 4 5 6
     * 1 2 3 ... 7 8 9
     * 3 4 5 ... 10 11 12
     *
     * @return java.util.List<java.lang.String>
     * @author alpha
     * @since 2024/03/27 21:20
     */
    public List<String> getPageList() {
        pageList = new ArrayList<>();
        totalPage = getTotalPage();
        //获取总的页数
        if (totalPage < 7) {
            //总共的记录不超过7条
            for (int i = 1; i < totalPage; i++) {
                pageList.add(i + "");
            }
        } else {
            //7条或7条以上的记录
            if (pageNum == 1 || pageNum == 2 || pageNum == 3) {
                pageList = Arrays.asList("1", "2", "3", "...", totalPage + "");
            } else {
                pageList = Arrays.asList((pageNum - 2) + "", (pageNum - 1) + "", pageNum + "", "..." + totalPage);
            }
        }
        System.out.println("pageList = " + pageList);
        return pageList;
    }

    /**
     * SQL分页中开始的位置
     *
     * @return int
     * @author alpha
     * @since 2024/03/27 16:52
     */
    public int getStart() {
        return (this.getPageNum() - 1) * this.getPageSize();
    }

    /**
     * 结束的位置
     *
     * @param
     * @return void
     * @author alpha
     * @since 2024/03/27 16:53
     */
    public int getEnd() {
        return this.getPageNum() * this.getPageSize();
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public String getKey() {
        return key;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getTotalPage() {
        //总的记录数除以每一页显示的条数
        totalPage = (int) Math.ceil(((double) totalCount) / pageSize);
        return totalPage;
    }

    public List<?> getList() {
        return list;
    }
}
